/*
 * Copyright 2010 Google Inc.
 * Copyright 2011 Thomas Amsler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.teleportdroid;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver implements ApplicationConstants {

	 private static final String TAG = "C2DMReceiver";
	
	public C2DMReceiver() {

		super(DeviceRegistrar.SENDER_ID);
	}

	@Override
	public void onRegistered(Context context, String registration) {

		DeviceRegistrar.registerWithServer(context, registration);
	}

	@Override
	public void onUnregistered(Context context) {

		SharedPreferences prefs = Prefs.get(context);
		String deviceRegistrationID = prefs.getString(DEVICE_REGISTRATION_ID, null);
		DeviceRegistrar.unregisterWithServer(context, deviceRegistrationID);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Bundle extras = intent.getExtras();
		if (extras != null) {
			String sms = (String) extras.get("sms");
			String debug = (String) extras.get("debug");

			if (debug != null) {
				// server-controlled debug - the server wants to know
				// we received the message, and when. This is not user-controllable,
				// we don't want extra traffic on the server or phone. Server may
				// turn this on for a small percentage of requests or for users
				// who report issues.
				DefaultHttpClient client = new DefaultHttpClient();
				HttpGet get = new HttpGet(AppEngineClient.BASE_URL + "/debug?id=" + extras.get("collapse_key"));
				// No auth - the purpose is only to generate a log/confirm delivery
				// (to avoid overhead of getting the token)
				try {
					client.execute(get);
				} catch (ClientProtocolException e) {
					// ignore
				} catch (IOException e) {
					// ignore
				}
			}
			
			if(null != sms) {
				Log.i(TAG, "SMS = " + sms);
			}
		}
	}

	@Override
	public void onError(Context context, String errorId) {
		context.sendBroadcast(new Intent(UPDATE_UI_INTENT));
	}

}
